<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'operating:paymentConfirmation:edit'"
        >
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.userName }}
        </a>
      </template>
      <template #img="{ record }">
        <TableImg :size="60" :simpleShow="true" :imgList="record.proofs" />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsOperatingPaymentConfirmationList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useGlobSetting } from '/@/hooks/setting';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    paymentConfirmationDelete,
    paymentConfirmationListData,
    shenhe,
    hexiao,
  } from '/@/api/operating/paymentConfirmation';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { TableImg } from '/@/components/Table';

  const { t } = useI18n('operating.paymentConfirmation');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('回款确认管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('状态'),
        field: 'auditStatus',
        component: 'Select',
        componentProps: {
          dictType: 'repayment_is',
          allowClear: true,
        },
      },
      {
        label: t('主体'),
        field: 'caseSource',
        component: 'Select',
        componentProps: {
          dictType: 'upstream',
          allowClear: true,
        },
      },
      {
        label: t('还款日起'),
        field: 'repaymentTime_gte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('还款日止'),
        field: 'repaymentTime_lte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('负责人'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('还款主体'),
      dataIndex: 'caseSource',
      key: 'a.case_source',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('债务人'),
      dataIndex: 'dtbtor',
      key: 'a.dtbtor',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('还款账户信息'),
      dataIndex: 'accountInfo',
      key: 'a.account_info',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('还款时间'),
      dataIndex: 'repaymentTime',
      key: 'a.repayment_time',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('还款金额'),
      dataIndex: 'repaymentMoney',
      key: 'a.repayment_money',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: '还款凭证',
      dataIndex: 'proofs',
      helpMessage: ['这是简单模式的图片列表'],
      width: 140,
      slots: { customRender: 'img' }, // 5.1.0 及之前
      slot: 'img', // 5.2.0+ （Antdv 3.x 不再使用 slots）
    },
    {
      title: t('审批人'),
      dataIndex: 'approver',
      key: 'a.approver',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'auditStatus',
      key: 'a.audit_status',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-ant-design:check-outlined',
        color: 'success',
        title: t('确认入账'),
        popConfirm: {
          title: t('是否确认该付款已入账'),
          confirm: handleLingqu.bind(this, record),
        },
        ifShow() {
          // eslint-disable-next-line prettier/prettier
          return record.auditStatus === '待确认'||record.auditStatus === '暂时核销'
        },
      },
      {
        icon: 'i-simple-line-icons:question',
        color: 'success',
        title: t('暂核'),
        popConfirm: {
          title: t('是否暂时核销'),
          confirm: zhanhexiao.bind(this, record),
        },
        ifShow() {
          return record.auditStatus === '待确认';
        },
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除'),
        popConfirm: {
          title: t('是否确认删除'),
          confirm: handleDelete.bind(this, record),
        },
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: paymentConfirmationListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  async function handleLingqu(record: Recordable) {
    const params = { id: record.id };
    const res = await shenhe(params);
    showMessage(res.message);
    handleSuccess(record);
  }
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/operating/paymentConfirmation/exportData',
      params: getForm().getFieldsValue(),
    });
  }
  async function zhanhexiao(record: Recordable) {
    const params = { id: record.id };
    const res = await hexiao(params);
    showMessage(res.message);
    handleSuccess(record);
  }
  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await paymentConfirmationDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
